package cn.edu.lzu.service.impl;

import cn.edu.lzu.common.assist.DataPage;
import cn.edu.lzu.common.assist.LabExamineResultAssist;
import cn.edu.lzu.common.assist.ResponseResult;
import cn.edu.lzu.dao.ExamineItemMapper;
import cn.edu.lzu.dao.LabExamineClauseMapper;
import cn.edu.lzu.dao.LabItemValueMapper;
import cn.edu.lzu.entity.LabExamineClause;
import cn.edu.lzu.entity.LabItemValue;
import cn.edu.lzu.service.LabExamineClauseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @PackageName: cn.edu.lzu.service.impl
 * @ClassName: LabExamineClauseServiceImpl
 * @Description: TODO
 * @Author: 张琦[QQ:3098086691]
 * @Date: 2020/3/21 16:09
 * @Version: 1.0
 */
@Service
@Transactional
public class LabExamineClauseServiceImpl implements LabExamineClauseService {
    @Resource
    private LabExamineClauseMapper labExamineClauseMapper;
    @Resource
    private ExamineItemMapper examineItemMapper;
    @Resource
    private LabItemValueMapper labItemValueMapper;

    @Override
    public DataPage getLabExamineClausesByUnitId(long unitId, Integer examineItemId) {
        List<LabExamineClause> labExamineClauseList = labExamineClauseMapper.selByUnitId(unitId, examineItemId);
        DataPage dataPage = new DataPage();
        //如果没有异常
        if (labExamineClauseList.size() >= 0) {
            dataPage.setMsg("成功");
            dataPage.setCode(0);
            dataPage.setData(labExamineClauseList);
            dataPage.setTotalCount(labExamineClauseList.size());
        } else {
            dataPage.setMsg("失败");
            dataPage.setCode(1);
            dataPage.setData(new ArrayList());
            dataPage.setTotalCount(0);
        }
        return dataPage;

    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 添加一条实验室检查项目, 然后修改该条目所在项目的状态为“已添加”
     * @Date: 2020/3/21 21:11
     * @Param: [labExamineClause]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @Override
    public ResponseResult addLabExamineClause(LabExamineClause labExamineClause) {
        ResponseResult responseResult = new ResponseResult();
        int result = labExamineClauseMapper.insLabExamineClause(labExamineClause);
        if (result == 1) {
            //修改该条目所在项目的状态为“已添加”
            int itemResult = examineItemMapper.updSetFillStatusTrue(labExamineClause.getExamineItemId());
            if (itemResult == 1) {
                responseResult.setCode(1);
                responseResult.setMsg("添加成功！");
            } else {
                responseResult.setCode(0);
                responseResult.setMsg("添加失败！");
            }
        } else {
            responseResult.setCode(0);
            responseResult.setMsg("添加失败！");
        }
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据Id删除相应的条目
     * @Date: 2020/3/21 22:15
     * @Param: [id]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @Override
    public ResponseResult delLabExamineClauseById(int id) {
        int result = labExamineClauseMapper.updDeleteClauseById(id);
        ResponseResult responseResult = new ResponseResult();
        if (result == 1) {
            responseResult.setCode(1);
            responseResult.setMsg("删除成功！");
        } else {
            responseResult.setCode(0);
            responseResult.setMsg("删除失败");
        }
        return responseResult;
    }

    @Override
    public DataPage getFuzzySearchLabClauses(long unitId, String searchInput, Integer searchType, Integer examineItemId) {
        System.out.println("searchInput: "+searchInput+"\t"+"searchType:"+searchType);
        DataPage dataPage = new DataPage();
        List<LabExamineClause> labExamineClauseList=null;
        //如果输入不为空，执行模糊搜索
        if (searchInput != null && searchInput!= "") {
            //根据clauseName搜索
            if (searchType == 1) {
                labExamineClauseList=labExamineClauseMapper.selFuzzyByLabClauseName(unitId, searchInput);

            } else if (searchType == 2) {
                //根据codeName搜索
                labExamineClauseList=labExamineClauseMapper.selFuzzyByLabClauseCodeName(unitId, searchInput);

            } else if (searchType == 3) {
                //根据所属项目名称搜索
                List<Integer> examineItemIdList=examineItemMapper.getExamineItemIdByName(unitId, searchInput);
                if(examineItemIdList.size()>0){
                    String examineItemIds="";
                    for(int i=0 ; i<examineItemIdList.size(); i++){
                        examineItemIds+=examineItemIds+examineItemIdList.get(i)+",";
                    }
                    examineItemIds=examineItemIds.substring(0, examineItemIds.length()-1);
                    labExamineClauseList=labExamineClauseMapper.selFuzzyByLabClauseExamineItemId(unitId, examineItemIds);
                }else{
                    labExamineClauseList=new ArrayList<>();
                }

            }
            dataPage.setData(labExamineClauseList);
            if(labExamineClauseList!=null){
                dataPage.setMsg("数据获取成功！");
                dataPage.setCode(0);
            }else{
                dataPage.setMsg("数据获取失败！");
                dataPage.setCode(1);
            }
            dataPage.setTotalCount(0);
            return dataPage;
        } else {
            //执行顺序搜索，根据currentPageNum和pageSize获取一页数据
            System.out.println("输入为空");
            dataPage=getLabExamineClausesByUnitId(unitId, examineItemId);
            return dataPage;
        }
    }

    @Override
    public Map<String, Object> getLabClauseByItemId(long unitId, int examineItemId, int fillItemId) {
        System.out.println("unitId:"+unitId+"\t"+"examineItemId:"+examineItemId+"\t"+"fillItemId:"+fillItemId);
        Map<String, Object> map=new LinkedHashMap<>();
        List<LabExamineClause> labExamineClauseList=labExamineClauseMapper.selByUnitId(unitId, examineItemId);
        for(int i=0; i<labExamineClauseList.size(); i++){
            LabItemValue labItemValue=labItemValueMapper.selLabItemValueByPrimary(fillItemId, labExamineClauseList.get(i).getId(), unitId);
            if(labItemValue==null){
                int result=labItemValueMapper.insAddRecurd(fillItemId, labExamineClauseList.get(i).getId(),unitId);
                if(result!=1){
                    return new HashMap<String, Object>();
                }
            }
            map.put(fillItemId+","+labExamineClauseList.get(i).getId(), labExamineClauseList.get(i));
        }
        return map;
    }

    @Override
    public DataPage getLabExamineResult(long unitId, int examineItemId, int fillItemId) {
        List<LabExamineClause> labExamineClauseList=labExamineClauseMapper.selByUnitId(unitId, examineItemId);
        List<LabExamineResultAssist> labExamineResultAssists=new ArrayList<>();
        for(int i=0; i<labExamineClauseList.size(); i++){
            LabExamineClause labExamineClause=labExamineClauseList.get(i);
            LabItemValue labItemValue=labItemValueMapper.selLabItemValueByPrimary(fillItemId, labExamineClause.getId(), unitId);
            System.out.println(labItemValue);

            LabExamineResultAssist labExamineResultAssist=new LabExamineResultAssist();
            labExamineResultAssist.setNumericalOrder(labExamineClause.getNumericalOrder());
            labExamineResultAssist.setCodeName(labExamineClause.getCodeName());
            labExamineResultAssist.setClauseName(labExamineClause.getClauseName());
            labExamineResultAssist.setReferenceValue(labExamineClause.getReferenceValue());
            labExamineResultAssist.setUnits(labExamineClause.getUnits());
            labExamineResultAssist.setValue(labItemValue.getValue());
            labExamineResultAssists.add(labExamineResultAssist);
        }
        DataPage dataPage=new DataPage();
        dataPage.setData(labExamineResultAssists);
        dataPage.setTotalCount(labExamineClauseList.size());
        dataPage.setCode(0);
        return dataPage;
    }
}
